package com.example.producer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

//发送消息
//简单模式
public class Producer_HelloWorld {
    public static void main(String[] args) throws IOException, TimeoutException {
//        1. 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
//        2. 设置参数
        connectionFactory.setHost("192.168.254.129");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/myHost");
        connectionFactory.setUsername("h790864074");
        connectionFactory.setPassword("h13834968356");
//        3. 获取连接
        Connection connection = connectionFactory.newConnection();
//        4. 创建Channel
        Channel channel = connection.createChannel();
//        5. 创建一个队列
//        queue 队列名
//        durable 是否持久化--当mq重启后队列时候还在
//        exclusive: 是否独占
//        autoDelete: 当没有Consumer 是否自动删除
//        arguments:参数
        channel.queueDeclare("hello world", true, false, false, null);
//        6. 发送消息(简单模式下交换机名字为“” 注意routingkey必须和队列名一样才能publish)
        channel.basicPublish("", "hello world", null, "hello world".getBytes(StandardCharsets.UTF_8));
//        释放资源
//        channel.close();
//        connection.close();
    }
}
